﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;

namespace MyDAL.SQL
{
    public class SQLModelsBuilder
    {
        public static void Create(string dbName,string conn,MyDAL.Enumeration.ConnStringType type)
        {

            DataTable ds=SQL.DBSchema.SelectDataBaseTable (dbName,conn,type);

            foreach (DataRow dr in ds.Rows)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("using System;");
                sb.AppendLine("using System.Collections.Generic;");
                sb.AppendLine("using System.Text;");
                sb.AppendLine("namespace Model");
                sb.AppendLine("{");
                sb.AppendLine("    public class "+dr[0]);
                sb.AppendLine("    {");
                DataTable dt = SQL.DBSchema.SelectDataTableColumns(dr[0].ToString (),conn, type);
                foreach (DataRow d in dt.Rows)
                {
                    sb.AppendLine("       private "+ GetType(d[1].ToString()) +" _"+d[0]+";");
                    sb.AppendLine("       public " + GetType(d[1].ToString()) + " " + d[0]  );
                    sb.AppendLine("       {");
                    sb.AppendLine("            get{return _" + d[0] + ";}");
                    sb.AppendLine("            set{_" + d[0] + " = value;}"); 
                    sb.AppendLine("       }");
                    sb.AppendLine("");
                }
                sb.AppendLine("    }");
                sb.AppendLine("}");

                string baseRoot=AppDomain.CurrentDomain.BaseDirectory+"Models";
                if(!Directory .Exists(baseRoot))
                {
                    Directory.CreateDirectory(baseRoot);
                }
                FileStream fs= File.Create(baseRoot + "\\M_" + dr[0] + ".cs");
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(sb.ToString ());
                sw.Close();
                sw.Dispose();
                fs.Close();
                fs.Dispose();
            }

           
        }

        public static string GetType(string sqlType)
        {
            switch (sqlType)
            {
                case "varchar": return "string";

                case "char": return "string";

                case "text": return "string";

                case "datetime": return "DateTime";

                case "money": return "decimal";

                case "decimal": return "decimal";

                case "numeric": return "decimal";

                case "smalldatetime": return "DateTime";  
                   
                case "bigint": return "Int64";

                case "smallint": return "Int16";

                case "int": return "int";

                case "bit": return "bool";

                case "float": return "float";

                case "tinyint": return "Int16";

                case "nvarchar": return "string";

                case "image": return "string";

                default: return "string";

            }
        }
    }
}
